package com.hdoit.ares.web.policy.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.insu.model.FanhuaCarBusinessRiskKinds;
import com.hdoit.ares.logical.insu.model.ZhonganCar;
import com.hdoit.ares.logical.insu.service.IFanhuaAccidentService;
import com.hdoit.ares.logical.insu.service.IFanhuaCarBusinessRiskKindsService;
import com.hdoit.ares.logical.insu.service.IFanhuaCarService;
import com.hdoit.ares.logical.insu.service.IZhonganAccidentService;
import com.hdoit.ares.logical.insu.service.IZhonganCarService;
import com.hdoit.ares.logical.policy.model.ListPolicy;
import com.hdoit.ares.logical.policy.service.IListPolicyService;
import com.hdoit.ares.logical.viewmodel.policy.ListPolicySearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author caochengmeng
 * @email
 * @createtime 2018年01月22日 09:08:39
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/policy/listPolicy/")
public class ListPolicyController extends BaseController {
	@Override
	public String getRoutePath() {
		return "areas/admin/policy/listPolicy/";
	}

	private IListPolicyService listPolicyService;

	public IListPolicyService getListPolicyService() {
		return listPolicyService;
	}

	@Resource
	public void setListPolicyService(IListPolicyService listPolicyService) {
		this.listPolicyService = listPolicyService;
	}

	private IFanhuaAccidentService fanhuaAccidentService;
	private IZhonganAccidentService zhonganAccidentService;
	private IFanhuaCarService fanhuaCarService;
	private IZhonganCarService zhonganCarService;
	private IFanhuaCarBusinessRiskKindsService fanhuaCarBusinessRiskKindsService;

	public IFanhuaAccidentService getFanhuaAccidentService() {
		return fanhuaAccidentService;
	}

	@Resource
	public void setFanhuaAccidentService(IFanhuaAccidentService fanhuaAccidentService) {
		this.fanhuaAccidentService = fanhuaAccidentService;
	}

	public IZhonganAccidentService getZhonganAccidentService() {
		return zhonganAccidentService;
	}

	@Resource
	public void setZhonganAccidentService(IZhonganAccidentService zhonganAccidentService) {
		this.zhonganAccidentService = zhonganAccidentService;
	}

	public IFanhuaCarService getFanhuaCarService() {
		return fanhuaCarService;
	}

	@Resource
	public void setFanhuaCarService(IFanhuaCarService fanhuaCarService) {
		this.fanhuaCarService = fanhuaCarService;
	}

	public IZhonganCarService getZhonganCarService() {
		return zhonganCarService;
	}

	@Resource
	public void setZhonganCarService(IZhonganCarService zhonganCarService) {
		this.zhonganCarService = zhonganCarService;
	}

	public IFanhuaCarBusinessRiskKindsService getFanhuaCarBusinessRiskKindsService() {
		return fanhuaCarBusinessRiskKindsService;
	}

	@Resource
	public void setFanhuaCarBusinessRiskKindsService(IFanhuaCarBusinessRiskKindsService fanhuaCarBusinessRiskKindsService) {
		this.fanhuaCarBusinessRiskKindsService = fanhuaCarBusinessRiskKindsService;
	}

	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "listPolicyDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";

	@RequestMapping(value = indexViewName, method = { RequestMethod.GET, RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel, ListPolicySearch search, boolean ajaxPost) {

		pagerModel.setTotalAmount(listPolicyService.queryCount(search));
		List<ListPolicy> listPolicys = listPolicyService.queryList(search, pagerModel);

		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("listPolicys", listPolicys);
		model.addAttribute("listPolicySearch", search);

		if (ajaxPost) {
			return view(outputViewName);
		}

		return view(indexViewName);
	}

	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET })
	public String listPolicyDetailed(Model model, String key) {
		ListPolicy listPolicy = new ListPolicy();
		if (!StringUtils.isEmpty(key)) {
			listPolicy = listPolicyService.queryModel(key);
			String signs = listPolicy.getSigns();
			String oid = listPolicy.getOid();
			if (signs.equals("fanhuaAccident")) {
				listPolicy = listPolicyService.queryFanhuaAccidentDetail(oid);
			}
			if (signs.equals("zhonganAccident")) {
				listPolicy = listPolicyService.queryZhonganAccidentDetail(oid);
			}
			if (signs.equals("fanhuaCar")) {
				listPolicy = listPolicyService.queryFanhuaCarDetail(oid);
				
				List<FanhuaCarBusinessRiskKinds> fanhuaCarBusinessRiskKinds = fanhuaCarBusinessRiskKindsService.queryModelByOid(listPolicy.getFanhuaCar().getId());
				
				model.addAttribute("fanhuaCarBusinessRiskKinds",fanhuaCarBusinessRiskKinds);
			}
			if (signs.equals("zhonganCar")) {
				listPolicy = listPolicyService.queryZhonganCarDetail(oid);
				ZhonganCar zhongancar = zhonganCarService.queryModelByPolicyNo(oid);
				String vehiclePolicyOrderNo = zhongancar.getVehiclePolicyOrderNo();
				ZhonganCar zhonganCarBusiness = zhonganCarService.queryModelByVehiclePolicyOrderNo(vehiclePolicyOrderNo, "1");
				model.addAttribute("zhonganCarBusiness", zhonganCarBusiness);
				if (!StringUtils.isEmpty(vehiclePolicyOrderNo)) {
					ZhonganCar zhonganCar = zhonganCarService.queryModelByVehiclePolicyOrderNo(vehiclePolicyOrderNo, "2");
					model.addAttribute("zhonganCar", zhonganCar);
				}else {
					ZhonganCar zhonganCar = new ZhonganCar();
					model.addAttribute("zhonganCar",zhonganCar);
				}
			}
			model.addAttribute("listPolicy", listPolicy);
		}
		return view(detailedViewName);
	}

	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model, ListPolicy listPolicy) {
		if (StringUtils.isEmpty(listPolicy.getId())) {
			listPolicy.setModelStatus(ModelStatus.add);
		} else {
			listPolicy.setModelStatus(ModelStatus.update);
		}

		ActionResult actionResult;
		if (listPolicyService.applyChange(listPolicy)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}

		model.addAttribute("listPolicy", listPolicy);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);

		return view(detailedViewName);
	}

	@RequestMapping(value = deleteViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public @ResponseBody String delete(String key) {
		if (listPolicyService.deleteFakeByKey(key)) {
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
}